package cn.school.logistics.service.impl;

import cn.school.common.exception.ApiException;
import cn.school.logistics.dao.AssetsDao;
import cn.school.logistics.dao.AssetsStoreDetilsDao;
import cn.school.logistics.entity.Assets;
import cn.school.logistics.entity.AssetsStoreDetils;
import cn.school.logistics.service.AssetsStoreDetilsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 资产明细ServiceImpl
 *
 * @className: AssetsStoreDetilsServiceImpl
 * @author: fangjin.tao
 * @date: 2023/2/17
 **/
@Service
public class AssetsStoreDetilsServiceImpl extends ServiceImpl<AssetsStoreDetilsDao, AssetsStoreDetils> implements AssetsStoreDetilsService {
	@Autowired
	private AssetsDao assetsDao;
	
	
	@Override
	@Transactional
	public boolean saveBiz(AssetsStoreDetils sku) {
		// 库存明细处理（调剂）前
		Assets teachingMaterial = this.assetsDao.selectById(sku.getAsseId());
		sku.setAsdeProcNotCount(teachingMaterial.getAsseCount()); // 处理前库存
		
		// 更新教材实时库存;
		int skucount = "1".equals(sku.getAsdeType()) ? sku.getAsdeCount() : - sku.getAsdeCount();
		this.assetsDao.updateSku(sku.getAsseId(), skucount);
		
		// 库存明细处理(调剂)后
		teachingMaterial = this.assetsDao.selectById(sku.getAsseId());
		sku.setAsdeProcAfterCount(teachingMaterial.getAsseCount());
		if (teachingMaterial.getAsseCount() < 0 ) {
			throw new ApiException("不允许出库，库存不够！");
		}
		sku.setAsseCode(teachingMaterial.getAsseCode());
		sku.setAsseName(teachingMaterial.getAsseName());
		super.save(sku);
		
		return true;
	}
}
